﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SmartMotion.Core.Mgrs;
using Sunny.UI;
using WeifenLuo.WinFormsUI.Docking;

namespace SmartMotion.Pages.DockPages
{
    public partial class FrmVar : DockContent
    {
        private ConcurrentDictionary<string, VarInfo> _vars;
        public FrmVar()
        {
            InitializeComponent();
        }

        private void FrmVar_Load(object sender, EventArgs e)
        {
            HideOnClose = true;
            _vars = VarMgr.Ins.Vars[VarMgr.FlowVars];
            RefreshData();
        }

        private void 增加变量ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var varInfo = new VarInfo();
            var typeList = new List<string>()
            {
                typeof(string).FullName,
                typeof(int).FullName,
                typeof(bool).FullName,
                typeof(double).FullName,
                typeof(short).FullName,
                typeof(ushort).FullName,
                typeof(uint).FullName,
                typeof(long).FullName,
                typeof(ulong).FullName,
                typeof(float).FullName,
            };
            int selIndex = 0;
            if (this.ShowSelectDialog(ref selIndex,typeList))
            {
                varInfo.Type = typeList[selIndex];
            }
            retry:
            string varName = string.Empty;
            if (UIInputDialog.InputStringDialog(ref varName,true,"请输入变量名称"))
            {
                if (_vars.ContainsKey(varName))
                {
                    UIMessageBox.ShowError($"变量{varName}已存在，请重新输入");
                    goto retry;
                }

                varInfo.Name = varName;
            }
            string groupName = "Default";
            if (UIInputDialog.InputStringDialog(ref groupName, false, "请输入分组名"))
            {
                if (string.IsNullOrEmpty(groupName))
                {
                    groupName = "Default";
                }
            }
            varInfo.Group = groupName;
            string value = string.Empty;
            if (UIInputDialog.InputStringDialog(ref value,true,"请输入变量初始值"))
            {
                dynamic dyValue = null;
                if (varInfo.Type==typeof(bool).FullName)
                {
                    dyValue = Convert.ToBoolean(value);
                }
                if (varInfo.Type == typeof(short).FullName)
                {
                    dyValue = Convert.ToInt16(value);
                }
                if (varInfo.Type == typeof(ushort).FullName)
                {
                    dyValue = Convert.ToUInt16(value);
                }
                if (varInfo.Type == typeof(int).FullName)
                {
                    dyValue = Convert.ToInt32(value);
                }
                if (varInfo.Type == typeof(uint).FullName)
                {
                    dyValue = Convert.ToUInt32(value);
                }
                if (varInfo.Type == typeof(long).FullName)
                {
                    dyValue = Convert.ToInt64(value);
                }
                if (varInfo.Type == typeof(ulong).FullName)
                {
                    dyValue = Convert.ToUInt64(value);
                }
                if (varInfo.Type == typeof(double).FullName)
                {
                    dyValue = Convert.ToDouble(value);
                }
                if (varInfo.Type == typeof(float).FullName)
                {
                    dyValue = Convert.ToSingle(value);
                }
                if (varInfo.Type == typeof(string).FullName)
                {
                    dyValue =value;
                }

                varInfo.Value = dyValue;
                varInfo.InitValue = dyValue;
                _vars.TryAdd(varInfo.Name, varInfo);

                RefreshData();

            }

        }

        private void RefreshData()
        {
            var bs = new BindingSource();
            if (_vars!=null)
            {
                bs.DataSource = _vars.Values;
                grid.DataSource = bs;
            }
        }

        private void 删除变量ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (grid.CurrentRow?.DataBoundItem is VarInfo varinfo)
            {
                _vars.TryRemove(varinfo.Name, out var varInfoOut);
                RefreshData();
            }
        }

        private void 增加对象ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var varInfo = new VarInfo();
            var typeList = new List<string>()
            {
                typeof(object).FullName,


            };
            int selIndex = 0;
            if (this.ShowSelectDialog(ref selIndex,typeList))
            {
                varInfo.Type = typeList[selIndex];

            }
            lblRetry:
            string objName = string.Empty;
            if (UIInputDialog.InputStringDialog(ref objName,true,"请输入对象名称"))
            {
                if (_vars.ContainsKey(objName))
                {
                    UIMessageBox.ShowError($"对象{objName}已存在，请重新输入一个对象名称");
                    goto lblRetry;
                }
                varInfo.Name = objName;
            }
            else
            {
                return;
            }

            string group = "Default";
            if (UIInputDialog.InputStringDialog(ref  group,false,"请输入分组名称，不输入为默认分组"))
            {
                if (string.IsNullOrEmpty(group))
                {
                    group = "Default";
                }
            }
            varInfo.Group = group;
            varInfo.InitValue = null;
            varInfo.Value = null;
            _vars.TryAdd(varInfo.Name, varInfo);
            RefreshData();

        }
    }
}
